package com.newenergy.wisehome.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.util.ImageUtil;
import com.newenergy.wisehome.data.bean.DeviceCategory;

import java.util.List;

/**
 * Created by kyx on 2018/7/13.
 */

public class ScanHEAAdapter extends RecyclerArrayAdapter<DeviceCategory> {

    private Context mContext;
    private ItemOperCallBack mItemOperCallBack;

    public void refreshData(List<DeviceCategory> data) {
        clear();
        addAll(data);
        notifyDataSetChanged();
    }


    public ScanHEAAdapter(Context context) {
        super(context);
        mContext = context.getApplicationContext();
    }



    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(parent);

    }


    private class ViewHolder extends BaseViewHolder<DeviceCategory> {

        private TextView tvName;
        private ImageView ivAddDevice;
        private ImageView ivIcon;


        public ViewHolder(ViewGroup parent) {
            super(parent, R.layout.list_scan_hea);
            tvName = $(R.id.tv_name);
            ivAddDevice = $(R.id.iv_add_device);
            ivIcon = $(R.id.iv_icon);
        }


        @Override
        public void setData(DeviceCategory data) {
            super.setData(data);
            tvName.setText(data.getName());
            int imageId = ImageUtil.getImageId(data.getIcName());
            Glide.with(mContext).load(imageId).diskCacheStrategy(DiskCacheStrategy.NONE).into(ivIcon);

            ivAddDevice.setOnClickListener(v -> {
                if (mItemOperCallBack!=null){
                    mItemOperCallBack.notifyAdd(getDataPosition());
                }

            });
        }
    }


    public void setItemOperCallBack(ItemOperCallBack itemOperCallBack) {
        mItemOperCallBack = itemOperCallBack;
    }


    public interface ItemOperCallBack {
        void notifyAdd(int position);

    }


}
